package com.anolesoft.twelvePlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.twelvePlan.dto.MesHistoryFabinfoDTO;
import com.anolesoft.twelvePlan.entity.MesHistoryFabinfoDO;
import com.anolesoft.twelvePlan.mapper.MesGyInfoMapper;
import com.anolesoft.twelvePlan.mapper.MesHistoryFabinfoMapper;
import com.anolesoft.twelvePlan.service.MesHistoryFabinfoService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyq
 * @date 2022/3/5
 */
@Service
@Slf4j
public class MesHistoryFabinfoServiceImpl implements MesHistoryFabinfoService {
    @Autowired
    private MesHistoryFabinfoMapper mesHistoryFabinfoMapper;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public PageData<List<MesHistoryFabinfoDTO>> queryHistory(MesHistoryFabinfoDTO mesHistoryFabinfoDTO, Integer pageNo, Integer pageSize) {
        Page<MesHistoryFabinfoDO> page = new Page<>(pageNo, pageSize);
        List<MesHistoryFabinfoDO> mesHistoryFabinfoDOList = mesHistoryFabinfoMapper.queryHistoryFabinfo(MesHistoryFabinfoDO.builder()
                .fabrecordid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getFabrecordid()))
                .partid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getPartid()))
                .factoryId(CommonUtils.stringNull(mesHistoryFabinfoDTO.getFactoryId()))
                .build(), page);
        List<MesHistoryFabinfoDTO> mesHistoryFabinfoDTOList = mesHistoryFabinfoDOList.stream()
                .map(t -> BeanUtil.copyProperties(t, MesHistoryFabinfoDTO.class))
                .collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), mesHistoryFabinfoDTOList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertHistory(MesHistoryFabinfoDTO mesHistoryFabinfoDTO) {
        int repeat = mesHistoryFabinfoMapper.repeatHistory(MesHistoryFabinfoDO.builder()
                .fabrecordid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getFabrecordid()))
                .build());
        int count = 0;
        if (repeat == 0) {
            count = mesHistoryFabinfoMapper.insertHistoryFabinfo(MesHistoryFabinfoDO.builder()
                    .objuid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getObjuid()))
                    .fabrecordid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getFabrecordid()))
                    .partid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getPartid()))
                    .jgnum(mesHistoryFabinfoDTO.getJgnum())
                    .worker(CommonUtils.stringNull(mesHistoryFabinfoDTO.getWorker()))
                    .worktype(CommonUtils.stringNull(mesHistoryFabinfoDTO.getWorktype()))
                    .createuid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getCreateuid()))
                    .factoryId(CommonUtils.stringNull(mesHistoryFabinfoDTO.getFactoryId()))
                    .build());
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateHistory(MesHistoryFabinfoDTO mesHistoryFabinfoDTO) {
        int repeat = mesHistoryFabinfoMapper.repeatHistory(MesHistoryFabinfoDO.builder()
                .objuid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getObjuid()))
                .fabrecordid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getFabrecordid()))
                .build());
        int count = 0;
        if (repeat == 0) {
            count = mesHistoryFabinfoMapper.updateHistoryFabinfo(MesHistoryFabinfoDO.builder()
                    .objuid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getObjuid()))
                    .fabrecordid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getFabrecordid()))
                    .partid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getPartid()))
                    .jgnum(mesHistoryFabinfoDTO.getJgnum())
                    .worker(CommonUtils.stringNull(mesHistoryFabinfoDTO.getWorker()))
                    .worktype(CommonUtils.stringNull(mesHistoryFabinfoDTO.getWorktype()))
                    .modifyuid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getModifyuid()))
                    .build());
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteHistory(MesHistoryFabinfoDTO mesHistoryFabinfoDTO) {
        int count = mesHistoryFabinfoMapper.deleteHistoryFabinfo(MesHistoryFabinfoDO.builder()
                .objuid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getObjuid()))
                .deleteuid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getDeleteuid()))
                .build());
        return count;
    }

    @Override
    public int exportHistory(MesHistoryFabinfoDTO mesHistoryFabinfoDTO) {
        int count = mesHistoryFabinfoMapper.exportHistoryFabinfo(MesHistoryFabinfoDO.builder()
                .fabrecordid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getFabrecordid()))
                .partid(CommonUtils.stringNull(mesHistoryFabinfoDTO.getPartid()))
                .factoryId(CommonUtils.stringNull(mesHistoryFabinfoDTO.getFactoryId()))
                .build());
        return count;
    }

    @Override
    public void uploadHistory(List<MesHistoryFabinfoDO> list) {
        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
        MesHistoryFabinfoMapper mesHistoryFabinfoMapper = sqlSession.getMapper(MesHistoryFabinfoMapper.class);
        list.forEach(a -> {
            mesHistoryFabinfoMapper.insert(a);
        });
        sqlSession.commit();
    }
}
